package com.zjitc.pigspring.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.Goods;
import com.zjitc.pigspring.bean.po.Type;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/getAll")
    public Result<List<Type>> getAll(){
        List<Type> types = typeService.list();
        if (types!=null){
            return Result.success(types);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Type> selectTypeById(@PathVariable Integer id){
        Type type = typeService.getById(id);
        if(type!=null){
            return Result.success(type);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addType(@RequestBody Type type){
        QueryWrapper<Type> wrapper = new QueryWrapper<Type>()
                .eq("name",type.getName());
        Type one = typeService.getOne(wrapper);
        if(one!=null){
            return Result.error(Codes.TYPE_EXISTS);
        }
        boolean save = typeService.save(type);
        if(save){
            return Result.success(null, Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @DeleteMapping("/deleteOne/{id}")
    public Result deleteType(@PathVariable Integer id){
        boolean flag = typeService.removeById(id);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<Goods>()
                .eq("type_id", id);
        List<Goods> list = goodsService.list(goodsQueryWrapper);
        boolean flag1 = true;

        for (Goods goods : list) {
            if (goodsService.removeById(goods)) {
                flag1 = new SellerController().deleteData(goods.getId());
                if (!flag1) {
                    break;
                }
            }
        }
        if (flag1 && flag) {
            return Result.success(Codes.DELETE_YES);
        }

        return Result.error(Codes.DELETE_NO);
    }

    @PostMapping("/deleteAll")
    public Result deleteAllType(@RequestBody List<Integer> ids){
        boolean flag = typeService.removeBatchByIds(ids);
        boolean flag1 = true;
        for (Integer id : ids) {
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<Goods>()
                    .eq("type_id", id);
            List<Goods> list = goodsService.list(goodsQueryWrapper);
            for (Goods goods : list) {
                if (goodsService.removeById(goods)) {
                    flag1 = new SellerController().deleteData(goods.getId());
                    if (!flag1) {
                        break;
                    }
                }
            }
            if (!flag1) {
                break;
            }

        }
        if (flag && flag1) {
            return Result.success(Codes.DELETE_YES);
        } else {
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PutMapping("/updateOne")
    public Result updateType(@RequestBody Type type){
        QueryWrapper<Type> wrapper = new QueryWrapper<Type>()
                .eq("name",type.getName());
        Type one = typeService.getOne(wrapper);
        if(one!=null && !one.getName().equals(type.getName())){
            return Result.error(Codes.TYPE_EXISTS);
        }
        boolean flag = typeService.updateById(type);
        if(flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<Type>> queryType(@RequestBody Type type){

        QueryWrapper<Type> queryWrapper = new QueryWrapper<Type>()
                .eq(StringUtils.isNotBlank(type.getName()),"name",type.getName());
        List<Type> list = typeService.list(queryWrapper);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }
}
